<template>
   <div class="app-container">
      <el-form  class="query-wrapper" :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
         <el-form-item   prop="dictName">
            <el-input
               v-model="queryParams.dictName"
               placeholder="Dict Name"
               clearable
               style="width: 240px"
               @keyup.enter="handleQuery"
            />
         </el-form-item>
         <el-form-item   prop="dictType">
            <el-input
               v-model="queryParams.dictType"
               placeholder="Dict Type"
               clearable
               style="width: 240px"
               @keyup.enter="handleQuery"
            />
         </el-form-item>
         <!-- <el-form-item label="状态" prop="status">
            <el-select
               v-model="queryParams.status"
               placeholder="字典状态"
               clearable
               style="width: 240px"
            >
               <el-option
                  v-for="dict in sys_normal_disable"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
               />
            </el-select>
         </el-form-item> -->          
         <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">Search</el-button>
            <el-button icon="Refresh" @click="resetQuery"></el-button>
         </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
         <el-col :span="1.5"  v-hasPermi="['dictData:edit']" >
            <el-button
               type="primary"
               plain
               icon="Plus"
               @click="handleAdd" 
            >Add</el-button>
         </el-col>
          
         <!-- <el-col :span="1.5">
            <el-button
               type="warning"
               plain
               icon="Download"
               @click="handleExport"
               v-hasPermi="['dictData:edit']"
            >导出</el-button>
         </el-col> -->
         <!-- <el-col :span="1.5">
            <el-button
               type="danger"
               plain
               icon="Refresh"
               @click="handleRefreshCache"
               v-hasPermi="['dictData:edit']"
            >刷新缓存</el-button>
         </el-col> -->
         <!-- <right-toolbar  @queryTable="getList"></right-toolbar> -->
      </el-row>

      <el-table v-loading="loading" :data="typeList">
         <el-table-column label="Dict No" align="center" prop="dictId" />
         <el-table-column label="Dict Name" align="center" prop="dictName" :show-overflow-tooltip="true"/>
         <el-table-column label="Dict Type" align="center" show-overflow-tooltip>
            <template #default="scope">
               <router-link :to="{path:'/dict/index', query:{dictType: scope.row.dictType,dictId:scope.row.dictId}}"  class="link-type">
                  <span>{{ scope.row.dictType }}</span>
               </router-link>
            </template>
         </el-table-column>
   
         <el-table-column label="Remark" align="center" prop="remark" :show-overflow-tooltip="true" />
         <el-table-column label="createTime" align="center" prop="createTime" width="180"/> 
         <el-table-column label="Operation" align="center" width="180" class-name="small-padding fixed-width"  v-hasPermi="['dictData:edit']">
            <template #default="scope">
               <el-button link type="primary"   @click="handleUpdate(scope.row)" v-if="hasAuth(auth, 'edit')">Edit</el-button>
               <el-button link type="primary"  @click="handleDelete(scope.row)" v-if="hasAuth(auth, 'edit')">Remove</el-button>
            </template>
         </el-table-column>
      </el-table>

      <pagination
         v-show="total > 0"
         :total="total"
         v-model:page="queryParams.pageNum"
         v-model:limit="queryParams.pageSize"
         @pagination="getList"
         style="float:right"
      />

      <!-- 添加或修改参数配置对话框 -->
      <el-dialog :title="title" v-model="open" width="500px" append-to-body draggable>
         <el-form ref="dictRef" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="Dict Name" prop="dictName">
               <el-input v-model="form.dictName" placeholder="Dict Name" />
            </el-form-item>
            <el-form-item label="Dict Type" prop="dictType">
               <el-input v-model="form.dictType" placeholder="Please input" />
            </el-form-item>
 
            <el-form-item label="Remark" prop="remark">
               <el-input v-model="form.remark" type="textarea" placeholder="Please input"></el-input>
            </el-form-item>
         </el-form>
         <template #footer>
            <div class="dialog-footer">
               <el-button type="primary" @click="submitForm">Confirm</el-button>
               <el-button @click="cancel">Cancel</el-button>
            </div>
         </template>
      </el-dialog>
   </div>
</template>

<script setup name="dictData">  
import service from '@/utils/request' 
import { hasAuth,getCurrentRouteAuth } from "@/utils/auth"  
import { ref,  getCurrentInstance, reactive,toRefs  } from "vue";
let auth = getCurrentRouteAuth()
const typeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);
const statuss = ["正常","停用"]
const { proxy } = getCurrentInstance();
 
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    dictName: undefined,
    dictType: undefined,
    status: undefined
  },
  rules: {
    dictName: [{ required: true, message: "Dict Name can't be null", trigger: "blur" }],
    dictType: [{ required: true, message: "Dict Type can't be null", trigger: "blur" }]
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询字典类型列表 */
const getList =async() => {
  loading.value = true;
  let result = await service.get('shipping/sysDictType', {params: queryParams.value})
  if(result){
      typeList.value  = result.data
      total.value = result.total
      loading.value = false;
  }
   
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    dictId: undefined,
    dictName: undefined,
    dictType: undefined,
    status: "0",
    remark: undefined
  };
  proxy.resetForm("dictRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
//   dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "Add Dict";
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.dictId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset(); 
   form.value = row;
   open.value = true;
   title.value = "Update Dict"; 
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["dictRef"].validate(valid => {
    if (valid) {
      if (form.value.dictId != undefined) {
        service.put("/shipping/sysDictType",form.value).then(response => {
          proxy.$modal.msgSuccess("Update Success!");
          open.value = false;
          getList();
        });
      } else {
          service.post("/shipping/sysDictType",form.value).then(response => {
          proxy.$modal.msgSuccess("Add Success");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const dictIds = row.dictId ;
  proxy.$modal.confirm('confirm to remove the dictId "' + dictIds + '"?').then(function() {
    return service.delete(`/shipping/sysDictType/${row.dictId}`);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("remove success!");
  }).catch(() => {});
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download("system/dict/type/export", {
    ...queryParams.value
  }, `dict_${new Date().getTime()}.xlsx`);
}
/** 刷新缓存按钮操作 */
function handleRefreshCache() {
  refreshCache().then(() => {
    proxy.$modal.msgSuccess("refresh success!");
    useDictStore().cleanDict();
  });
}

getList();
</script>
